<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单新增事件</title>
</head>
<body>
    <form action="">
        手机号: <input type="text" id="username" pattern="^1[3456789]\d+{9}">
        邮箱: <input type="email" id="email">
        <input type="submit" value="提交">
    </form>
    
    <script>
        // oninput: 监听当前元素内容的改变(增 删)，只要发生改变就会触发这个事件
        document.getElementById("username").oninput = function() {
            console.log(this.name);
        }

        // onkeyup: 键盘弹起时触发，每弹起一次触发一次
        document.getElementById("username").onkeyup = function() {
            console.log(this.name);
        }
        
        // oninvalid: 当验证不通过时触发
        document.getElementById("username").oninvalid = function() {
            console.log(this.name);
        }
    </script>
</body>
</html>
